# coding=utf-8
'''
Created on 2013-12-27

RESTful 接口，对外提供服务

@author: sunxiang
'''
from handler.base import BaseHandler
from service.user import UserService

class Restful(BaseHandler):
    def __init__(self, *argc, **argkw):
        super(Restful, self).__init__(*argc, **argkw)
        self.user = 'user' # 用户信息
    
    def get(self, key):
        if not key:
            return
        
        if key == self.user:
            userHandler = UserRestHandler()
            userHandler.handler(self)
        
        
class UserRestHandler(object):
    '''用户restful接口处理类
    '''
    
    def __init__(self):
        self.uuid_key = 'uuid'
        self.nickname_key = 'nickname'
        
    def handler(self, request):
        uuid = request.get_arguments(self.uuid_key)
        
        user_service = UserService()
        if uuid:
            user_info = user_service.get_user_by_uuid(uuid[0]);
        else:    
            nickname = request.get_arguments(self.nickname_key)
            print(nickname)
            if not nickname:
                raise Exception('比如填写uuid或nickname')
            
            user_info = user_service.get_user_by_nickname(nickname[0])
            
        if user_info:
            request.render_from_string(user_info[0].to_json())
        
class RestNone(BaseHandler): 
    def get(self):
        pass
    